package com.carful.xigua.modules.user.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.carful.v10.entity.HyInfo;
import com.carful.v10.service.V10Service;
import com.carful.xigua.modules.common.entity.Rest;
import com.carful.xigua.modules.common.entity.RestBody;
import com.carful.xigua.modules.common.util.SecurityContextUtils;
import com.carful.xigua.modules.user.dto.request.ReqPassword;
import com.carful.xigua.modules.user.dto.request.ReqUser;
import com.carful.xigua.modules.user.dto.response.ResRecord;
import com.carful.xigua.modules.user.dto.response.ResUser;
import com.carful.xigua.modules.user.entity.Record;
import com.carful.xigua.modules.user.entity.User;
import com.carful.xigua.modules.user.service.impl.RecordServiceImpl;
import com.carful.xigua.modules.user.service.impl.UserServiceImpl;
import io.github.linpeilie.Converter;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * user/user
 *
 * @author ad
 * @since 2024-07-11
 */
@RestController
@RequestMapping("/user/user")
public class UserController {


    @Autowired
    private UserServiceImpl userService;

    @Autowired
    private Converter converter;

    @Autowired
    private V10Service v10Service;

    @Autowired
    private RecordServiceImpl recordService;

    /**
     * 获取用户信息
     * @return
     */
    @GetMapping("/info")
    public ResUser getUserInfo() {
        User user = SecurityContextUtils.getLoginUserDetails().getUser();

        ResUser resUser = converter.convert(user, ResUser.class);

        List<HyInfo> memberInfo = v10Service.getMemberInfo(user.getUserPhone(), user.getMember());
        if (memberInfo != null && memberInfo.size() != 0) {
            userService.sortHyInfo(memberInfo);
            userService.info(user, memberInfo);
            resUser.setHyInfo(memberInfo.get(0));
        }

        resUser.setTotalCount(user.getCnt());
        resUser.setCurrentCount(user.getCurrentCnt());
        resUser.setRecords(converter.convert(recordService.getList(user), ResRecord.class));

        resUser.setCheckCount((int) recordService.count(new LambdaQueryWrapper<Record>().eq(Record::getUserId, user.getId())));
        return resUser;
    }

    /**
     * 绑定手机号
     */
    @GetMapping("/bindPhone")
    public Rest bindPhone(HttpServletRequest request,
                          @RequestParam("phone") String phone,
                          @RequestParam("phoneCode") String phoneCode) {
        User user = SecurityContextUtils.getLoginUserDetails().getUser();

        String res = userService.bindPhone(user, request.getHeader("token"), phone, phoneCode);
        return RestBody.ok(res);
    }

    /**
     * 修改信息
     * @param reqUser
     * @return
     */
    @Deprecated
    @PutMapping("/modify")
    public String updUser(@RequestBody ReqUser reqUser) {
        reqUser.setPassword(null);
        boolean b = userService.updateById(converter.convert(reqUser, User.class));
        return b ? "修改成功" : "修改失败";
    }

    /**
     * 修改密码
     * @param reqPassword
     * @return
     */
    @PostMapping("/modify")
    public Boolean updPassword(@RequestBody ReqPassword reqPassword) {
        boolean b = userService.updPassword(reqPassword.getOldPassword(), reqPassword.getNewPassword());
        return b;
    }


    /**
     * 获取西瓜 code
     * @return
     */
    @GetMapping("/xiguaCode")
    public Rest getXiguaCode() {
        User user = SecurityContextUtils.getLoginUserDetails().getUser();

        String code = userService.getXiguaCode(user.getUsername());
        return RestBody.okData(code);
    }
}
